package cn.uc.grapics;

import java.util.ArrayList;
import java.util.List;

import javax.mail.internet.NewsAddress;

import cn.uc.util.GraphicsUtil;

import android.R.integer;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;

public class Scene {
    private GraphicsView parent;
    private Rect iViewPort = new Rect();
//    private Rect iSceneRect = new Rect();
    private int iWidth;
    private int iHeight;
    private Point iOffset = new Point();
    private List<GraphicsItem> items = new ArrayList<GraphicsItem>();
    
    public Scene(GraphicsView parent){
        this.parent = parent;
    }
    
    public void addItem(GraphicsItem aItem){
        items.add(aItem);
    }
    
    public boolean touchEvent(MotionEvent aEvent){
        return false;
    }
    
    public void paint(Canvas aCanvas){
        for(int i = 0; i < items.size(); i++){
            GraphicsItem sItem = items.get(i);
            if(GraphicsUtil.isRectCross(iViewPort, sItem.boundingRect())){
                sItem.paintEx(aCanvas);
            }
        }
    }
    
    public void setViewPort(Rect aRect){
        iViewPort = aRect;
    }
    
    public void setViewPortSize(int aw, int ah){
        iViewPort = new Rect(iOffset.x, iOffset.y, aw, ah);
        if(iWidth < aw){
            iWidth = aw;
        }
        
        if(iHeight < ah){
            iHeight = ah;
        }
    }    
   
    public void resize(int aw, int ah){
        iWidth = aw;
        iHeight = ah;
    }
    
    public int getSceneWidth(){
        return iWidth;
    }
    
    public int getSceneHeight(){
        return iHeight;
    }
    
    public boolean onTouchEvent(MotionEvent aEvent){
        GraphicsItem sItem = null;
        switch (aEvent.getAction()){
        case MotionEvent.ACTION_DOWN:
            sItem = getItemAt((int)aEvent.getX(), (int)aEvent.getY());
            if(sItem != null){
                removeFocus();
                sItem.setFocus(true);
            }
            break;

        case MotionEvent.ACTION_MOVE:
            sItem = getFocusItem();
            break;

        case MotionEvent.ACTION_UP:
            sItem = getFocusItem();
            break;
        }
        
        if(sItem != null){
            return sItem.onTouchEvent(aEvent);
        }
        
        return false;

    }
    
    public GraphicsItem getFocusItem(){
        for(int i = 0; i < items.size(); i++){
            GraphicsItem item = items.get(i);
            if(item.isFocus()){
                return item; 
            }
        }
        
        return null;
    }
    
    public GraphicsItem getItemAt(int ax, int ay){
        ArrayList<GraphicsItem> sItems = getItemsAt(ax, ay);
        if(sItems.size() > 0){
            return sItems.get(0);
        }
        
        return null;
    }
    
    public ArrayList<GraphicsItem> getItemsAt(int ax, int ay){
        ArrayList<GraphicsItem> sItems = new ArrayList<GraphicsItem>();
        for(int i = 0; i < items.size(); i++){
            GraphicsItem item = items.get(i);
            if(item.boundingRect.contains(ax, ay)){
                sItems.add(item);
            }
        }
        return sItems;
    }
    
    private void removeFocus(){
        for(int i = 0; i < items.size(); i++){
            items.get(i).setFocus(false);
        }
    }
}
